// import axios from 'axios'
import { config } from '@/config'
import { getUrlParams } from '@/utils/util'
import { getOpenId, h5GetUnionId } from '@/utils/api'
export default {
    install(Vue) {
        const agentId = getUrlParams()['agentId']
        if (agentId) {
            localStorage.agentId = agentId.replace(/[^0-9]/ig, '')
        }
        if (localStorage.openid && localStorage.openid !== 'null' && localStorage.openid !== 'undefined') {
            // 已经缓存openid~
            const shareUid = getUrlParams()['shareUid']
            if (shareUid) {
                localStorage.shareUid = shareUid.replace(/[^0-9]/ig, '')
            }
        } else {
            const code = getUrlParams()['code']
            const shareUid = getUrlParams()['shareUid']
            const local = window.location.href
            if (!code) {
                let authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${config.appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=${shareUid}#wechat_redirect`
                window.location.href = authUrl
            } else {
                // getOpenId(code).then((res) => {
                //   localStorage.openid = res.data && res.data.t
                //   location.replace(window.location.origin + window.location.hash)
                // })
                const shareUid = getUrlParams()['state']
                localStorage.shareUid = shareUid.replace(/[^0-9]/ig, '')
                h5GetUnionId(code).then((res) => {
                    localStorage.unionid = res.data && res.data.result.unionId
                    localStorage.openid = res.data && res.data.result.openId
                })
            }
        }
    }
}